import axios from "axios";
import { WebSocketClient } from "./websocket";
import messageStore from '../stores/csr/MessageStore'
import userInf from '../stores/user'

const instance = axios.create({
  baseURL: '/api',
  timeout: 10000,
});


// 请求拦截器
instance.interceptors.request.use(function (config) {
  // 在发送请求之前做些什么
  // 每次发送请求之前，向请求头中添加token信息
  config.headers.Authorization = sessionStorage.getItem('token')
  return config;
}, function (error) {
  // 对请求错误做些什么
  return Promise.reject(error);
});

// 响应拦截器
instance.interceptors.response.use(function (response) {
  // 对响应数据做点什么
  return response;
}, function (error) {
  // 对响应错误做点什么
  return Promise.reject(error);
});

export const $update =async (url,formData={})=>{
  const response = await instance.put(url,formData);
  return response.data;
};

export const $addHotel =async (url,formData={})=>{
  const response = await instance.post(url,formData);
  return response.data;
};

// get 请求方法
export const $get = async (url, params = {}) => {
  const response = await instance.get(url, { params });
  return response.data;
};

// post 请求方法
export const $delete = async (url, params = {}) => {
  const response = await instance.delete(url, params);
  return response.data;
};

// post 请求方法
export const $post = async (url, params = {}) => {
  const response = await instance.post(url, params);
  return response.data;
};

// deled 请求方法
export const $delet = async (url, params = {}) => {
  const response = await instance.delete(url, {
    data:params
  });
  return response.data;
};
// put 请求方法
export const $put = async (url, params = {}) => {
  const response = await instance.put(url, params);
  return response.data;
};

export const $Login = async (params)=>{
  let ret = await instance.post("/auth/login/root",{
    id: 0,
    username: "fysyyds668",
    password: "12345678",
    email: "",
    phoneNumber: "",
    fullName: "",
    sex: "",
    registrationDate: "",
    lastLogin: "",
    isRoot: ""})
  if(ret.data.code==200){

    ElNotification({
      title: '通知',
      message: ret.data.message,
      type: 'success',
    })
    // 登录成功后，将token信息保存到浏览器缓存中
    sessionStorage.setItem('token',ret.data.data.token)

    const userIf=userInf()
    userIf.setUser({id:ret.data.data.root.id})

    const mes=messageStore()
    mes.buildUserList()
    mes.setPark(true)
    mes.buildMessageRecord()

    return true
  }else{
    ElNotification({
      title: '通知',
      message: ret.message,
      type: 'error',
    })
    return false
  }
}

// 获取一个账户
export const $getOne = async (params) => {
  let ret = await $get('Admin/GetOne',params)
  return ret
}

export const deletehotle=(fromData)=>instance.delete("/hotel/delete",fromData)


export default instance






